package com.demo.androidgame.finsternis;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import com.demo.androidgame.fx.FileIO;

public class Settings {
    public static boolean soundEnabled = true;
    public final static boolean[] stageActive = new boolean[] { 
    	true,	//1
    	false,	//2
    	false,	//3
    	false,	//4
    	false,	//5
    	false,	//6
    	false,	//7
    	false,	//8
    	false	//9
    	};
    public final static String file = "test.finsternis";

    public static void load(FileIO files) {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(files.readFile(file)));
            soundEnabled = Boolean.parseBoolean(in.readLine());
            for(int i = 0; i < stageActive.length; i++) {
            	stageActive[i] = Boolean.parseBoolean(in.readLine());
            }
        } catch (IOException e) {
            // :( It's ok we have defaults
        } catch (NumberFormatException e) {
            // :/ It's ok, defaults save our day
        } catch (Exception ex) {
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
    }

    public static void save(FileIO files) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(files.writeFile(file)));
            out.write(Boolean.toString(soundEnabled));
            out.write("\n");
            for(int i = 0; i < stageActive.length; i++) {
            	out.write(Boolean.toString(stageActive[i]));
                out.write("\n");
            }
        } catch (IOException e) {
        } catch (Exception ex) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }

    public static void stageActiveChange(boolean nextStageActive) {
        for(int i=1; i < stageActive.length; i++) {
            if(stageActive[i] != nextStageActive) {
                for(int j = stageActive.length-1; j > i; j--)
                	stageActive[j] = stageActive[j-1];
                stageActive[i] = nextStageActive;
                break;
            }
        }
    }
}
